package com.vogtec.fota.controller;

import com.vogtec.fota.entity.UpgradeTask.*;
import com.vogtec.fota.service.UpgradeTaskService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by wei on 2017/7/3.
 */
@Slf4j
@RestController
@RequestMapping("/sharingbike/v1.0/upgrade/fota")
public class UpgradeTaskController {

    @Autowired
    private UpgradeTaskService upgradeTaskService;

    /**
     * 新建升级任务
     * @param aut
     * @return
     */
    @PostMapping(value = "/task")
    public AddUpgradeTaskResult appUpgradeTask(@RequestBody AddUpgradeTask aut) {
        log.info("<<== AddUpgradeTask: " + aut.toString());
        AddUpgradeTaskResult autr = upgradeTaskService.addUpgradeTask(aut);
        log.info(">>== AddUpgradeTaskResult: " + autr.toString());
        return autr;
    }

    /**
     * 查询任务
     * @param id
     * @return
     */
    @GetMapping(value = "/task/{id}")
    public TaskQueryRusult taskQuery(@PathVariable Integer id) {
        log.info("<<== id :" + id);
        TaskQueryRusult tqr = upgradeTaskService.taskQuery(id);
        log.info(">>== TaskQueryRusult: " + tqr.toString());
        return tqr;
    }

    /**
     * 根据完成状态任务
     * @param taskStatus
     * @return
     */
    @GetMapping(value = "/tasks/{taskStatus}")
    public QueryStatusResult taskQueryByStatus(@PathVariable String taskStatus,
                                               @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                               @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        log.info("<<== taskStatus :" + taskStatus + ", pageNo :" + pageNo + ", pageSize :" +pageSize);
        QueryStatusResult qsr = upgradeTaskService.taskQueryByStatus(taskStatus,pageNo,pageSize);
        log.info(">>== QueryStatusResult: " + qsr.toString());
        return qsr;
    }

    /**
     * 显示所有任务
     * @param
     * @return
     */
    @GetMapping(value = "/tasks")
    public QueryAllResult queryAll(@RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        log.info("<<== pageNo :" + pageNo + ", pageSize :" +pageSize);
        QueryAllResult qar = upgradeTaskService.queryAll(pageNo,pageSize);
        log.info(">>== QueryAllResult: " + qar.toString());
        return qar;
    }

    /**
     * 启动任务
     * @param id
     * @return
     */
    @PostMapping(value = "/task/{id}/start")
    public StartResult startTask(@PathVariable Integer id) throws MqttException{
        log.info("<<== id :" + id);
        StartResult sr = upgradeTaskService.startTask(id);
        log.info(">>== StartResult: " + sr.toString());
        return sr;
    }

    /**
     * 结束任务
     * @param id
     * @return
     */
    @PostMapping(value = "/task/{id}/finish")
    public FinishResult finishTask(@PathVariable Integer id) {
        log.info("<<== id :" + id);
        FinishResult fr = upgradeTaskService.finishTask(id);
        log.info(">>== FinishResult: " + fr.toString());
        return fr;
    }

    /**
     * 删除任务
     * @param id
     * @return
     */
    @DeleteMapping(value = "/task/{id}")
    public DeleteResult deleteTask(@PathVariable Integer id) {
        log.info("<<== id :" + id);
        DeleteResult dr = upgradeTaskService.deleteTask(id);
        log.info(">>== DeleteResult: " + dr.toString());
        return dr;
    }
}